import {defineStore} from "pinia"
// 操作请求
import {reqLogin,reqUserAll,reqUserList,reqUserAdd,reqUserById,reqUserUpdata,reqUserDelete} from "@/api/user"
// 登录用户数据类型
import type {
  /** 登录 */
  loginFormData,
  loginResponseData,
  userInfoData,
  /** 用户界面 */
  userListFormData,
  userAllResponseData,
  userListResponseData,
} from "@/api/user/type"
// 登录用户封装信息
import {UserState} from "./types/type"
// 用户库
let useUserStore = defineStore("User",{
  state:():UserState=>{
    return {
      // token:localStorage.getItem("VUE3-TOKEN"),
      user:JSON.parse((localStorage.getItem("VUE3-USER") as string)),
      users:[]
    }
  },
  actions:{
    // 登录
    async userLogin(data:loginFormData){
      // 接收登录请求
      let result:loginResponseData = await reqLogin(data);
      if(result.status === 0){
        // 断言数据类型
        this.user = (result.data as userInfoData)
        // 存储登录用户数据(持久化)
        localStorage.setItem("VUE3-USER",(JSON.stringify(result.data) as string))
        return "OK"
      }else{
        return Promise.reject(new Error(result.msg))
      }
    },
    // 移除本地存储的登录信息
    removeUser(){
      this.user = null;
      localStorage.removeItem("VUE3-USER")
    },
    // 获取所有用户请求
    async getUserAll(){
      // 接收请求
      let result:userAllResponseData = await reqUserAll();
      if(result.status === 0){
        // 断言数据类型以及存储
        this.users = (result.data as userInfoData[])
        return "OK"
      }else{
        return Promise.reject(new Error(result.msg))
      }
    },
    // 获取筛选后的用户请求(条件)
    async getUserList(data:userListFormData)
    {
      // 接收请求
      let result:userListResponseData = await reqUserList(data);
      console.log(result);
      
      if(result.status === 0){
        // 断言数据类型以及存储
        this.users = (result.data?.data as userInfoData[])
        // 返回成功数据
        return result.data
      }else{
        return Promise.reject(new Error(result.msg))
      }
    },
    // 添加
    async addUser(data:userInfoData){
      let result:loginResponseData =await reqUserAdd(data);
      if(result.status ==0){
      return "OK"
      }
      else{
        return Promise.reject (new Error(result.msg))
      }
      },
    // ID获取
    async getUserById(_id:string){
      let result:loginResponseData =await reqUserById(_id)
        if(result.status ===0){
        return result.data
        }else{
        return Promise.reject(new Error(result.msg))

        }
    },
    // 修改
    async updateUser(data:userInfoData){
      let result:loginResponseData =await reqUserUpdata(data);
        if(result.status ==0){
        return "OK"
        }
        else{
        return Promise.reject(new Error(result.msg))
        }
    },
    // 删除
    async deleteUser(_id:string){
      let result:loginResponseData =await reqUserDelete(_id);
        if(result.status ==0){
        return "OK"
        }
        else{
        return Promise.reject(new Error(result.msg))
        }
    },
        
  getters:{
    
  }
}
})
export default useUserStore